package top.mask.mq.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import top.mask.mq.utils.RabbitMQUtil;

import java.util.Scanner;

/**
 * 消息在手动应答时是不丢失的，放回队列重新消费
 */
@Slf4j
public class Task2 {
    //队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";

    @SneakyThrows
    public static void main(String[] args) {
        Channel channel = RabbitMQUtil.getChannel();
        //开启发布确认
        channel.confirmSelect();

        boolean durable = true; //需要让Queue持久化
        channel.queueDeclare(TASK_QUEUE_NAME,durable,false,false,null);

        //从控制台中输入信息
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String message = scanner.next();
            //设置生产者发送消息为持久化消息MessageProperties.PERSISTENT_TEXT_PLAIN
            channel.basicPublish("",TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes("UTF-8"));
            log.info("生产者发出的消息:[{}]",message);
        }

    }
}
